from docx import Document
from docx.shared import Pt
from docx.enum.section import WD_SECTION


def set_header_footer(section, header_text, footer_text):
    # 设置页眉
    header = section.header
    paragraph_header = header.paragraphs[0]
    run_header = paragraph_header.add_run()
    run_header.add_text(header_text)
    run_header.font.size = Pt(12)

    # 设置页脚
    footer = section.footer
    paragraph_footer = footer.paragraphs[0]
    run_footer = paragraph_footer.add_run()
    run_footer.add_text(footer_text)
    run_footer.font.size = Pt(12)


def copy_content(original_doc, new_section):
    # 遍历原始文档的段落
    for paragraph in original_doc.paragraphs:
        # 添加段落到新的节
        new_section.add_paragraph(paragraph.text, paragraph.style)


def main():
    # 创建一个新文档
    doc = Document()

    # 读取Word文档内容
    doc_path = r'C:\Users\Administrator\Desktop\科迪\哈电项目\需规\测试重点清单.docx'
    original_doc = Document(doc_path)

    # 获取文档的所有节
    for i, original_section in enumerate(original_doc.sections):
        # 创建一个新的节
        new_section = doc.add_section(WD_SECTION.NEW_PAGE)

        # 获取原始文档中的页眉和页脚
        header_text = original_section.header.paragraphs[0].text
        footer_text = original_section.footer.paragraphs[0].text

        # 在新节中设置不同的页眉和页脚
        set_header_footer(new_section, f'这是第{i + 1}页的页眉: {header_text}', f'这是第{i + 1}页的页脚: {footer_text}')

        # 复制原始文档中的内容到新文档
        copy_content(original_doc, new_section)

    # 保存文档
    doc.save('output_document.docx')


if __name__ == "__main__":
    main()
